﻿using System;
using System.Collections.Generic;

using HL.Lib.MVC;
using HL.Lib.Models;
using HL.Lib.Global;

namespace HL.Lib.CPControllers
{
    public class FormNewsPropertyController : CPController
    {
        public FormNewsPropertyController()
        {
            //khoi tao Service
            DataService = ModNewsPropertyService.Instance;
            CheckPermissions = true;
        }

        public void ActionIndex(ModNewsPropertyModel model)
        {
            // sap xep tu dong
            string orderBy = AutoSort(model.Sort);

            // tao danh sach
            var dbQuery = ModNewsPropertyService.Instance.CreateQuery()
                                .Where(model.NewsID > 0, o => o.NewsID == model.NewsID)
                                .Take(model.PageSize)
                                .OrderBy(orderBy)
                                .Skip(model.PageIndex * model.PageSize);

            ViewBag.Data = dbQuery.ToList();
            model.TotalRecord = dbQuery.TotalRecord;
            ViewBag.Model = model;
        }

        public void ActionAdd(ModNewsPropertyModel model)
        {
            string nid = string.Empty;
            var Product = ModNewsService.Instance.GetByID(model.NewsID);
            if (Product != null)
            {
                var menu = WebMenuService.Instance.GetByID(Product.MenuID);
                if (menu != null)
                {
                    var Property = ModPropertyService.Instance.GetByID(menu.PropertyID);                    
                    if (Property != null)
                    {
                        if (model.RecordID > 0)
                        {
                            entity = ModNewsPropertyService.Instance.GetByID(model.RecordID);
                            // khoi tao gia tri mac dinh khi update
                        }
                        else
                        {
                            entity = new ModNewsPropertyEntity();
                            //ViewBag.ListProperty = Property.GetChildByParentID();
                             //khoi tao gia tri mac dinh khi insert
                            entity.MenuID = Product.MenuID;
                            var listpp = ModNewsPropertyService.Instance.CreateQuery()
                            .Select(o => o.PropertyID)
                            .Where(o => o.NewsID == model.NewsID)
                            .ToList();
                            for (int i = 0; listpp != null && i < listpp.Count; i++)
                            {
                                nid += (nid == string.Empty ? "" : ",") + listpp[i].PropertyID;
                            }
                            ViewBag.ListProperty = Property.GetChildByParentID(nid);
                        }
                    }
                }
            }
            ViewBag.Data = entity;
            ViewBag.Model = model;
        }

        public void ActionSave(ModNewsPropertyModel model)
        {
            if (ValidSave(model))
               SaveRedirect();
        }

        public void ActionApply(ModNewsPropertyModel model)
        {
            if (ValidSave(model))
               ApplyRedirect(model.RecordID, entity.ID);
        }

        public void ActionSaveNew(ModNewsPropertyModel model)
        {
            if (ValidSave(model))
               SaveNewRedirect(model.RecordID, entity.ID);
        }

        #region private func

        private ModNewsPropertyEntity entity = null;

        private bool ValidSave(ModNewsPropertyModel model)
        {
            TryUpdateModel(entity);

            //chong hack
            entity.ID = model.RecordID;
            model.NewsID = model.NewsID;

            ViewBag.Data = entity;
            ViewBag.Model = model;

            CPViewPage.Message.MessageType = Message.MessageTypeEnum.Error;

            //kiem tra quyen han
            if ((model.RecordID < 1 && !CPViewPage.UserPermissions.Add) || (model.RecordID > 0 && !CPViewPage.UserPermissions.Edit))
                CPViewPage.Message.ListMessage.Add("Quyền hạn chế.");

            //kiem tra nhóm thuộc tính
            if (entity.PropertyID < 1)
                CPViewPage.Message.ListMessage.Add("Chọn nhóm thuộc tính.");

            if (entity.PropertyValueID < 1)
                CPViewPage.Message.ListMessage.Add("Chọn thuộc tính.");

            if (CPViewPage.Message.ListMessage.Count == 0)
            {
                try
                {
                    //save
                    var obj = ModNewsPropertyService.Instance.CreateQuery()
                        .Where(o => o.NewsID == model.NewsID && o.PropertyID == entity.PropertyID)
                        .ToSingle();
                    if (obj == null)
                    {
                        ModNewsPropertyService.Instance.Save(entity);
                    }
                    else if (obj.PropertyValueID == entity.PropertyValueID)
                    {
                        CPViewPage.Message.ListMessage.Add("Giá trị thuộc tính đã tồn tại");
                        return false;
                    }
                    else
                    {
                        //obj.PropertyValueID = entity.PropertyValueID;
                        ModNewsPropertyEntity obj2 = new ModNewsPropertyEntity();
                        obj2.NewsID = obj.NewsID;
                        obj2.PropertyID = obj.PropertyID;
                        obj2.PropertyValueID = entity.PropertyValueID;
                        ModNewsPropertyService.Instance.Save(obj2);
                    }
                }
                catch (Exception ex)
                {
                    Global.Error.Write(ex);
                    CPViewPage.Message.ListMessage.Add(ex.Message);
                    return false;
                }

                return true;
            }

            return false;
        }

        #endregion
    }

    public class ModNewsPropertyModel : DefaultModel
    {
        private int _LangID = 1;
        public int LangID
        {
            get { return _LangID; }
            set { _LangID = value; }
        }

        public int MenuID { get; set; }
        public int NewsID { get; set; }
    }
}

